package com.community.controller;

import com.community.pojo.ElderlyHealth;
import com.community.pojo.User;
import com.community.service.AdminElderlyHealthService;
import com.community.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class AdminHealthInformationController {
    private final AdminElderlyHealthService adminElderlyHealthService;
    private final AdminService adminService;

    @Autowired
    public AdminHealthInformationController(AdminElderlyHealthService adminElderlyHealthService, AdminService adminService) {
        this.adminElderlyHealthService = adminElderlyHealthService;
        this.adminService = adminService;
    }

    @GetMapping("/add-health-information-form")
    public String addHealthInformationFormController() {
        return "addHealthInformationForm";
    }

    @PostMapping("/add-health-information")
    public String addHealthInformationController(@ModelAttribute("elderlyHealth") ElderlyHealth elderlyHealth, Model model) {
        if(elderlyHealth.getUserId()==null){
            model.addAttribute("message","用户id不允许为空!");
            return "addHealthInformationForm";
        }
        int userId = elderlyHealth.getUserId();
        User user = adminService.findUser(userId);
        if (user == null) {
            model.addAttribute("message", "id为 " + userId + " 的用户不存在！");
            return "addHealthInformationForm";
        }
        ElderlyHealth findElderlyHealthByuserId= adminElderlyHealthService.findElderlyHealthByuserId(userId);
        if(findElderlyHealthByuserId!=null) {
            model.addAttribute("message", "用户ID为 " + userId + " 的健康信息已经存在！不允许重复录入");
            return "addHealthInformationForm";
        }
        int n = adminElderlyHealthService.addElderlyHealth(elderlyHealth);
        if(n == 1){
            int healthId = elderlyHealth.getHealthId();
            model.addAttribute("message", "注册成功，新注册的老龄人健康id为: " + healthId);
        } else {
            model.addAttribute("message", "注册失败!");
        }
        return "addHealthInformationResult";
    }

    @GetMapping("/find-health-information-form")
    public String findHealthInformationFormController() {
        return "findHealthInformationForm";
    }
    @PostMapping("/find-health-information")
    public String findHealthInformationController(@RequestParam String healthId, Model model) {
        if(healthId.equals("")){
            model.addAttribute("message","健康id不允许为空!");
            return "findHealthInformationForm";
        }
        int id=Integer.parseInt(healthId);
        ElderlyHealth findElderlyHealth= adminElderlyHealthService.findHealthById(id);
        if(findElderlyHealth==null){
            model.addAttribute("message","该id的健康信息不存在!");
            return "findHealthInformationForm";
        }else{
            model.addAttribute("findElderlyHealth",findElderlyHealth);
        }
        return "findHealthInformationResult";
    }

    @GetMapping("/delete-health-information-form")
    public String deleteHealthInformationFormController() {
        return "deleteHealthInformationForm";
    }
    @PostMapping("/delete-health-information")
    public String deleteHealthInformationController(@RequestParam String healthId, Model model) {
        if(healthId.equals("")){
            model.addAttribute("message","请输入要删除的健康信息的id!");
            return "deleteHealthInformationForm";
        }
        int id=Integer.parseInt(healthId);
        ElderlyHealth findElderlyHealth= adminElderlyHealthService.findHealthById(id);
        if(findElderlyHealth==null){
            model.addAttribute("message","该id的健康信息不存在，无法删除!");
            return "deleteHealthInformationForm";
        }
        int n = adminElderlyHealthService.deleteElderlyHealth(id);
        if(n == 1){
            model.addAttribute("message", "成功删除id为 " + id + " 的健康信息！");
        }else{
            model.addAttribute("message","删除失败!");
        }
        return "deleteHealthInformationResult";
    }

    @GetMapping("/update-health-information-form")
    public String updateHealthInformationFormController() {
        return "updateHealthInformationFormInputId";
    }
    @PostMapping("/update-health-information-input-id")
    public String updateHealthInformationInputIdController(@RequestParam String healthId, Model model) {
        if(healthId.equals("")){
            model.addAttribute("message","请输入要修改的健康信息的id!");
            return "updateHealthInformationFormInputId";
        }
        int id=Integer.parseInt(healthId);
        ElderlyHealth findElderlyHealth= adminElderlyHealthService.findHealthById(id);
        if(findElderlyHealth==null){
            model.addAttribute("message","该id的健康信息不存在，无法更新!");
            return "updateHealthInformationFormInputId";
        }
        model.addAttribute("findElderlyHealth",findElderlyHealth);
        return "updateHealthInformationForm";
    }
    @PostMapping("/update-health-information")
    public String updateHealthInformationController(
            @RequestParam Double height,
            @RequestParam Integer age,
            @RequestParam Double weight,
            @RequestParam String healthCondition,
            @RequestParam Integer healthId,
            Model model){
        int n= adminElderlyHealthService.updateElderlyHealth(height,age,weight,healthCondition,healthId);
        if(n==1){
            model.addAttribute("message","修改成功!");
        }else{
            model.addAttribute("message","修改失败!");
        }
        return "updateHealthInformationResult";
    }

}
